package com.fzrskj.cloud.free.user.task;

import com.fzrskj.cloud.free.core.enums.DicParamEnum;
import com.fzrskj.cloud.free.core.utils.CloudContextUtils;
import com.fzrskj.cloud.free.dic.utils.DicParamUtils;
import com.fzrskj.cloud.free.user.bean.UserDiyViewBean;
import com.fzrskj.cloud.free.user.utils.UserDiyUtils;
import com.fzrskj.cloud.free.user.utils.UserTaskUtils;
import com.fzrskj.cloud.free.user.bean.UserTask;
import ms.core.db.ConnectionContext;
import ms.core.tool.StrTool;

import javax.servlet.ServletContext;
import java.util.*;

/**
 * UserVHostTask.
 *
 * @author zj.
 *         Created on 2018/12/14 0014.
 */
public class UserDiyProTask extends TimerTask{
    private ServletContext sc = null;
    private static boolean running=false;

    /**
     * constructor method of CCoreTask.
     * @param sc sc.
     */
    public UserDiyProTask(ServletContext sc){
        this.sc = sc;
    }

    @Override
    public void run() {
        if (!running){
            try {
                running = true;
                this.sc.log("start running UserDiyProTask...");
                this.doTask();
                running = false;
                this.sc.log("UserDiyProTask runned.");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                ConnectionContext.closeAllConnection();
            }
        }
    }

    /**
     * do task.
     */
    private void doTask(){
        this.willExpireTransfer();
    }

    /**
     * 设置过期.
     */
    private void willExpireTransfer() {
        DicParamUtils dpUtils = new DicParamUtils();
        UserTaskUtils utUtils = new UserTaskUtils();
        CloudContextUtils.setAgentId(CloudContextUtils.DEF_AGENT_ID);
        String expiryDays = dpUtils.getParamValue(DicParamEnum.DIY_BUSINESS_RENEW_DAY.getVal(), 1);
        if (StrTool.isBlankStr(expiryDays)) {
            return;
        }
        UserDiyUtils diyUtils = new UserDiyUtils();
        String[] array = expiryDays.split(",");
        for(int i=0;i<array.length;i++){
            List<UserDiyViewBean> diyBean = diyUtils.queryExpiryDiy(Integer.valueOf(array[i]), "");
            for (UserDiyViewBean viewBean : diyBean){
                CloudContextUtils.setAgentId(viewBean.getAgentId());
                Map<String, Object> cdt = new HashMap<String, Object>();
                cdt.put("userId", viewBean.getUserId());
                cdt.put("businessId", viewBean.getProductName());
                cdt.put("sendTime", new Date());
                cdt.put("expiryDays", Integer.parseInt(array[i]));
                cdt.put("type", 1);
                UserTask userTask = utUtils.queryEndTime(cdt);
                if(userTask==null) {
                    //记录发送数据
                    UserTask ut = new UserTask();
                    ut.setProductCode(viewBean.getProductCode());
                    ut.setBusinessId(viewBean.getProductName());
                    ut.setExpiryDays(Integer.parseInt(array[i]));
                    ut.setUserId(viewBean.getUserId());
                    ut.setUserName(viewBean.getUserName());
                    CloudContextUtils.setAgentId(CloudContextUtils.DEF_AGENT_ID);

                    Boolean flag = diyUtils.sendAlert(new Long(viewBean.getId()).intValue(), "mail, sms");
                    if (flag){
                        ut.setSendTime(new Date());
                        ut.setType(1);
                        utUtils.addUserTask(ut);
                    }
                }
            }
        }

    }

}
